package tech.aistar.mapper;

import org.apache.ibatis.annotations.*;
import tech.aistar.pojo.Order;

import java.util.List;

@Mapper
public interface OrderMapper {

    @Insert("INSERT INTO order (order_no, user_id, tickets, tickets_type_id, quantity, " +
            "unit_price, visit_date, status, create_time, pay_time) " +
            "VALUES (#{orderNo}, #{userId}, #{tickets}, #{ticketsTypeId}, #{quantity}, " +
            "#{unitPrice}, #{visitDate}, #{status}, #{createTime}, #{payTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Order order);

    @Select("SELECT * FROM order WHERE id = #{id}")
    Order findById(Long id);

    @Select("SELECT * FROM order WHERE order_no = #{orderNo}")
    Order findByOrderNo(String orderNo);

    @Select("SELECT * FROM order WHERE user_id = #{userId} ORDER BY create_time DESC")
    List<Order> findByUserId(Long userId);

    @Select("SELECT * FROM order WHERE status = #{status}")
    List<Order> findByStatus(String status);

    @Update("UPDATE order SET status = #{status}, pay_time = #{payTime} WHERE id = #{id}")
    int updateStatus(Order order);

    @Delete("DELETE FROM order WHERE id = #{id}")
    int delete(Long id);

    @Select("SELECT COUNT(*) FROM order WHERE user_id = #{userId}")
    int countByUserId(Long userId);

    @Select("select * from `order`;")
    List<Order> findAll();
}


